package com.zhxt.web.controller.monitor;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhxt.common.constant.HttpStatus;
import com.zhxt.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhxt.common.annotation.Log;
import com.zhxt.common.core.controller.BaseController;
import com.zhxt.common.core.domain.AjaxResult;
import com.zhxt.common.core.page.TableDataInfo;
import com.zhxt.common.enums.BusinessType;
import com.zhxt.common.utils.poi.ExcelUtil;
import com.zhxt.framework.web.service.SysPasswordService;
import com.zhxt.system.domain.SysLogininfor;
import com.zhxt.system.service.ISysLogininforService;

/**
 * 系统访问记录
 *
 * @author zhxt
 */
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController
{
    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private SysPasswordService passwordService;

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLogininfor logininfor) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        if(null==logininfor){
            tableDataInfo.setCode(HttpStatus.NOT_FOUND);
            tableDataInfo.setMsg("参数为空");
        }
        startPage();
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        list.forEach(SysLogininfor::xss);
        if(list.isEmpty()){
            tableDataInfo.setCode(HttpStatus.NOT_FOUND);
            tableDataInfo.setMsg("查询失败");
            return tableDataInfo;
        }
        return getDataTable(list);
    }
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininfor logininfor) {
        // 参数校验
        if (logininfor == null) {
            throw new IllegalArgumentException("登录日志查询条件不能为空");
        }
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @PostMapping("/del/{infoIds}")
    public AjaxResult remove(@PathVariable Long[] infoIds) {
        if (infoIds == null || infoIds.length == 0) {
            return AjaxResult.error("要删除的登录信息为空");
        }
        try {
            int result = logininforService.deleteLogininforByIds(infoIds);
            if (result > 0) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error("删除登录信息失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("删除登录信息时发生异常");
        }
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @PostMapping("/clean")
    public AjaxResult clean()
    {
        logininforService.cleanLogininfor();
        return success();
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName) {
        if (StringUtils.isEmpty(userName)) {
            return AjaxResult.error("用户名不能为空");
        }
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
}
